# -*- coding: utf-8 -*-
import scrapy
from selenium import webdriver
from scrapy.xlib.pydispatch import dispatcher
from scrapy import signals
import time
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
from zhihu.utils.proxy_ip import GetIP


# class PazhihuSpider(scrapy.Spider):
#     name = 'pazhihu'
#     allowed_domains = ['zhihu.com']
#     start_urls = ['http://zhihu.com/']
#
#     def parse(self, response):
#         pass


class PazhihuSpider(scrapy.Spider):
    name = 'pazhihu'
    allowed_domains = ['www.zhihu.com']
    start_urls = ['https://www.zhihu.com/signup?next=%2F']

    def __init__(self):
        # 创建browser对象
        self.browser = webdriver.Chrome(r'/home/python/Desktop/chromedriver')
        super(PazhihuSpider, self).__init__()
        # 当spider关闭时传递信号给dispatcher.connect，
        # dispatcher.connect调用self.closeSpider关闭浏览器
        dispatcher.connect(self.closeSpider, signals.spider_closed)

    def closeSpider(self):
        # 关闭浏览器
        self.browser.quit()
        pass

    def parse(self, response):
        time.sleep(3)

        print(self.browser.page_source)

        # response.text()